<?php
require_once dirname(dirname(dirname(__FILE__))) . '/init.php';
/**
 *系统审核调拨申请
 */
class StoryTransferApplyAgree extends Controller_CronJob {
	public function run() {
		$stockTransferApplyModel = new Cron_Model_Backup_StockTransferApply();
		$applyAutoCheckRuleModel = new Cron_Model_Backup_TransferApplyAutoCheckRule();
		$waitCheckList = $stockTransferApplyModel->getWaitCheckApply();
		if (empty($waitCheckList)) {
			$msg = "调拨申请待审核列表为空！";
			$this->log->record($msg);
			exit($msg);
		}

		$failCheck = [];
		foreach ($waitCheckList as $waitCheckApply) {
			$applyId = $waitCheckApply['id'];
			try {
				$isConformAutoCheckRule = $applyAutoCheckRuleModel->isConform($waitCheckApply);
			} catch (Exception $e) {
				$failCheck[] = $applyId;
				$this->log->record("系统自动审核需求调拨申请:记录" . $applyId . "出错！，错误原因:" . $e->getMessage(), Log::WARN);
				continue;
			}
			if ($isConformAutoCheckRule) {
				$success = $stockTransferApplyModel->agreeBySystem($applyId);
				if (!$success) {
					$failCheck[] = $applyId;
					$this->log->record("系统自动审核需求调拨申请:记录" . $applyId . "出错！，错误原因:" . $stockTransferApplyModel->getErrorMsg(), Log::WARN);
				}
			}
		}
		if (empty($failCheck)) {
			$msg = date("Y-m-d") . "日系统自动审核需求调拨申请完成！";
			$this->log->record($msg);
			exit($msg);
		}
		exit('系统自动审核需求调拨申请有错误发生！错误详细见日志！');
	}
}

$storyTransferApplyAgree = new StoryTransferApplyAgree();
$storyTransferApplyAgree->run();